package pers.zb.cloud.service.upms.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.zb.cloud.service.upms.dao.UserInfoMapper;
import pers.zb.cloud.service.upms.entity.SysPermission;
import pers.zb.cloud.service.upms.entity.SysRoleClientAuthorize;
import pers.zb.cloud.service.upms.entity.SysRoleClientPermission;
import pers.zb.cloud.service.upms.entity.SysUser;
import pers.zb.cloud.service.upms.service.PermissionService;
import pers.zb.cloud.service.upms.service.RoleClientAuthorizeService;
import pers.zb.cloud.service.upms.service.RoleClientPermissionService;
import pers.zb.cloud.service.upms.service.UserService;
import pers.zb.cloud.service.upms.vo.UserAuthoritiesInfoVo;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户服务相关业务处理
 */
@Service("userServiceImpl")
public class UserServiceImpl extends BaseServiceImpl<SysUser> implements UserService {

    @Autowired
    private UserInfoMapper userMapper;

    @Autowired
    private RoleClientAuthorizeService roleClientAuthorizeService;

    @Autowired
    private RoleClientPermissionService roleClientPermissionService;

    @Autowired
    private PermissionService permissionService;


    /**
     * 根据登录账户名获取用户信息、权限
     */
    @Override
    public UserAuthoritiesInfoVo getUserInfoAndPermissionDetail(String loginAccountName) throws Exception {
        if(StringUtils.isEmpty(loginAccountName)){
            return null;
        }

        //获取用户信息
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("loginAccount",loginAccountName);
        SysUser userInfo = userMapper.selectOneByExample(example);

        if(userInfo == null){
            return null;
        }

        //获取用户拥有的所有权限
        List<String> permissionCode = new ArrayList<>();

        /**
         * 获取用户的角色在所有应用中拥有的权限
         */
        //用户的角色
//        List<SysRole> userRoleList = new ArrayList<>();
//        SysRole sysRole = new SysRole();
//        sysRole.setId(68043282935971840L);
//        userRoleList.add(sysRole);

        List<Long> roleIdList = new ArrayList<>();
        roleIdList.add(68043282935971840L);
        //获取角色能访问的应用
        List<SysRoleClientAuthorize> roleClientList =  roleClientAuthorizeService.getHaveAccessClientListByRoles(roleIdList);

        List<String> clientIdList = new ArrayList<>();
        for (SysRoleClientAuthorize roleClientAuthorize : roleClientList) {
            clientIdList.add(roleClientAuthorize.getClientId());
        }

        //获取角色对应可访问的应用中的所有可访问权限
        List<SysRoleClientPermission> roleClientPermissionList = roleClientPermissionService.getHaveAccessPermissionByRolesAndClients(roleIdList,clientIdList);

        List<Long> permissionIdList = new ArrayList<>();
        for (SysRoleClientPermission roleClientPermission : roleClientPermissionList) {
            permissionIdList.add(roleClientPermission.getPermissionId());
        }
        //获取权限
        List<SysPermission> permissionList1 = permissionService.getPermissionList(permissionIdList);

        for (SysPermission permission : permissionList1) {
            permissionCode.add(permission.getCode());
        }
//        permissionCode.add("BookList");
//        permissionCode.add("memberExport");

        UserAuthoritiesInfoVo userAuthoritiesInfoVo = new UserAuthoritiesInfoVo();
        userAuthoritiesInfoVo.setUserName(userInfo.getLoginAccount());
        userAuthoritiesInfoVo.setPassword(userInfo.getLoginPassword());
        userAuthoritiesInfoVo.setStatus(userInfo.getStatus());
        userAuthoritiesInfoVo.setPermissionCode(permissionCode);
        return userAuthoritiesInfoVo;
    }
}
